package com.cloudweb.oa.config;

import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.InstalledOfficeManagerHolder;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.core.office.OfficeUtils;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Arrays;

@Slf4j
@Configuration
@ConfigurationProperties(value = "jodconverter.local")
@ConditionalOnProperty(name = "jodconverter.local.enabled", matchIfMissing = true, havingValue = "true")
public class JodconverterConfig {

    private LocalOfficeManager officeManager;
    @Value("${jodconverter.local.officeHome}")
    private String officeHome;
    @Value("${jodconverter.local.hostName}")
    private String hostName;
    @Value("${jodconverter.local.portNumbers}")
    private String portNumbers;

    @Value("${jodconverter.local.maxTasksPerProcess}")
    private Integer maxTasksPerProcess;
    @Value("${jodconverter.local.processTimeout}")
    private Long processTimeout;

    /**
     * 启动Office组件进程
     *
     * @return
     */
    @PostConstruct
    public OfficeManager officeManager() {
        // 多个端口处理
        String[] portsString = portNumbers.split(",");
        int[] ports = Arrays.stream(portsString).mapToInt(Integer::parseInt).toArray();
        // 系统判断
        String os = System.getProperty("os.name").toLowerCase();

        officeManager = LocalOfficeManager.builder()
                // .officeHome(os.contains("windows") ? officeHome : "linuxHome")
                .officeHome(officeHome)
                .hostName(hostName)
                .portNumbers(ports)
                .processTimeout(processTimeout)
                .maxTasksPerProcess(maxTasksPerProcess)
                .install()
                .build();
        try {
            officeManager.start();
            InstalledOfficeManagerHolder.setInstance(officeManager);
            log.info("LibreOffice 进程启动成功");
        } catch (OfficeException e) {
            log.error("启动 LibreOffice 组件失败");
            throw new RuntimeException(e);
        }
        return officeManager;
    }

    /**
     * 创建DocumentConverter实例
     *
     * @return
     */
    @Bean(name = "documentConverter")
    public DocumentConverter documentConverter() {
        log.info("创建DocumentConverter实例");
        LocalConverter documentConverter = LocalConverter.builder()
                .officeManager(officeManager)
                .build();
        return documentConverter;
    }

    @PreDestroy
    public void destroyOfficeManager() {
        if (null != officeManager && officeManager.isRunning()) {
            log.info("终止office进程");
            OfficeUtils.stopQuietly(officeManager);
        }
    }
}